<template>
  <div class="admin-login-container">
    <!-- 动态背景装饰 -->
    <div class="background-pattern"></div>

    <!-- 主登录卡片 -->
    <div class="admin-login-card">
      <!-- 左侧装饰区域 -->
      <div class="admin-decorative-side">
        <div class="admin-logo">
          <img src="https://cdn-icons-png.flaticon.com/512/878/878052.png" alt="食尚先锋">
          <h1>食尚先锋</h1>
        </div>
        <img
            src="https://images.unsplash.com/photo-1555396273-367ea4eb4db5?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80"
            alt="餐厅工作场景"
            class="admin-side-image"
        >
      </div>

      <!-- 右侧登录表单 -->
      <div class="admin-login-form">
        <h2 class="admin-login-title">员工登录</h2>
        <p class="admin-login-subtitle">请输入您的凭证以访问管理系统</p>

        <el-form
            ref="loginForm"
            :model="form"
            :rules="rules"
            @keyup.enter="handleLogin"
        >
          <!-- 用户名输入 -->
          <el-form-item prop="username">
            <el-input
                v-model="form.username"
                placeholder="请输入用户名"
                :prefix-icon="User"
                size="large"
                class="admin-input"
            />
          </el-form-item>

          <!-- 密码输入 -->
          <el-form-item prop="password">
            <el-input
                v-model="form.password"
                type="password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
                size="large"
                class="admin-input"
            />
          </el-form-item>

          <!-- 登录按钮 -->
          <el-form-item>
            <el-button
                type="primary"
                class="admin-login-btn"
                :loading="loading"
                @click="handleLogin"
            >
              <span v-if="!loading">登录系统</span>
              <span v-else>正在验证...</span>
            </el-button>
          </el-form-item>
        </el-form>

        <div class="admin-footer">
          <el-link type="info" @click="forgotPassword">忘记密码?</el-link>
          <el-divider direction="vertical" />
          <el-link type="primary" @click="contactAdmin">联系管理员</el-link>
        </div>
      </div>
    </div>

    <!-- 版权信息 -->
    <div class="admin-copyright">
      © 2025 食尚先锋管理系统 | 员工后台
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import md5 from 'md5'
import { employee } from "@/api/employee";
import {setUserRole} from "@/utils/auth";

const router = useRouter()

// 表单数据对象
const form = reactive({
  username: '',
  password: ''
})

// 验证规则
const rules = reactive({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 3, message: '密码长度至少3位', trigger: 'blur' }
  ]
})

// 加载状态
const loading = ref(false)
const loginForm = ref(null)

// 登录处理
const handleLogin = async () => {
  try {
    await loginForm.value.validate()
    loading.value = true

    // MD5加密密码
    const params = {
      ...form,
      password: md5(form.password)
    }

    const res = await employee(params)
    if (res.code === "200") {
      ElMessage.success('登录成功')
      localStorage.setItem('token', res.data)
      setUserRole('admin')
      await router.push({ name: 'EmployeeList' })
    } else {
      ElMessage.error(res.msg || '登录失败')
    }
  } catch (error) {
    if (error.response) {
      const msg = error.response.data.msg || '登录失败'
      ElMessage.error(msg)
    } else {
      ElMessage.error('请求失败，请检查网络')
    }
  } finally {
    loading.value = false
  }
}

// 忘记密码
const forgotPassword = () => {
  ElMessage.info('请联系系统管理员重置密码')
}

// 联系管理员
const contactAdmin = () => {
  ElMessage.info('请拨打管理员电话: 191-4251-5795')
}
</script>

<style scoped>
.admin-login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  overflow: hidden;
  font-family: 'Helvetica Neue', Arial, sans-serif;
}

.background-pattern {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url('@/assets/images/img.png');
  background-size: cover;
  background-position: center;
  opacity: 1;
  z-index: 0;
}


.admin-login-card {
  width: 900px;
  height: 550px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

.admin-decorative-side {
  width: 45%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  color: white;
}

.admin-logo {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}

.admin-logo img {
  width: 40px;
  height: 40px;
  margin-right: 15px;
}

.admin-logo h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.admin-side-image {
  width: 100%;
  height: auto;
  border-radius: 8px;
  margin-top: auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  object-fit: cover;
  max-height: 300px;
}

.admin-login-form {
  width: 55%;
  padding: 60px 50px;
  display: flex;
  flex-direction: column;
}

.admin-login-title {
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 8px;
}

.admin-login-subtitle {
  font-size: 14px;
  color: #7f8c8d;
  margin-bottom: 40px;
}

.admin-input {
  margin-bottom: 20px;
}

:deep(.admin-input .el-input__wrapper) {
  border-radius: 8px;
  padding: 12px 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05) !important;
  border: 1px solid #e0e0e0;
  transition: all 0.3s ease;
}

:deep(.admin-input .el-input__wrapper:hover) {
  border-color: #667eea;
  box-shadow: 0 2px 12px rgba(102, 126, 234, 0.1) !important;
}

:deep(.admin-input .el-input__wrapper.is-focus) {
  border-color: #667eea;
  box-shadow: 0 2px 12px rgba(102, 126, 234, 0.2) !important;
}

.admin-login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  color: white;
  margin-top: 10px;
  transition: all 0.3s ease;
}

.admin-login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.admin-footer {
  margin-top: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
}

:deep(.admin-footer .el-divider--vertical) {
  margin: 0 15px;
  background-color: #e0e0e0;
}

.admin-copyright {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 12px;
  color: #7f8c8d;
  z-index: 1;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .admin-login-card {
    width: 90%;
    height: auto;
    flex-direction: column;
  }

  .admin-decorative-side,
  .admin-login-form {
    width: 100%;
  }

  .admin-decorative-side {
    padding: 30px;
    height: 200px;
  }

  .admin-side-image {
    display: none;
  }

  .admin-login-form {
    padding: 40px 30px;
  }
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.admin-login-form {
  animation: fadeIn 0.6s ease-out forwards;
}
</style>